<template>
    <form-item
        :designer="designer" 
        :field="field" 
        :rules="state.rules"
    >
        <el-select
            v-model="formModel[field.options.name]"
            :size="size"
            :disabled="disabled"
            :clearable="clearable"
            :placeholder="field.options.placeholder"
            :multiple="multiple"
            :multiple-limit="multipleLimit"
        >
            <el-option 
                v-for="item in field.options.optionItems"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            />
        </el-select>
    </form-item>
</template>

<script setup>
import {nextTick, onMounted, reactive,computed} from 'vue';
import FormItem from './form-item.vue';
import {useRules} from '@/hooks/useRules.js'
defineOptions({
    name:"xy-select"
})
const props = defineProps({
    designer:{
        type:Object,
        default:()=>({})
    },
    field:{
        type:Object,
        default:()=>({})
    },
    formModel:{
        type:Object,
        default:()=>({})
    }
})

const state = reactive(({
    fieldVal:'',
    rules:[]
}))

useRules(state,props.field)

const disabled = computed(()=>{
    return props.field.options.disabled
})

const size = computed(()=>{
    return props.field.options.size || 'default'
})

const clearable = computed(()=>{
    return props.field.options.clearable
})

const multiple = computed(()=>{
    return props.field.options.multiple
})

const multipleLimit = computed(()=>{
    return props.field.options.multipleLimit
})

const setFieldVal = val=>{
    props.formModel[props.field.options.name] = val
}

onMounted(()=>{
    if(props.field && props.field.options.defaultValue){
        // 存在默认值的情况下进行初始化
        setFieldVal(props.field.options.defaultValue)
    }

    nextTick(()=>{
        if(!props.designer) return
        props.designer.createFieldValFun(props.field.id, setFieldVal)
    })
})
</script>

<style lang="scss" scoped>
</style>
